package com.zzyy.study.controller;

import com.zzyy.study.common.Result;
import com.zzyy.study.common.ResultUtils;
import com.zzyy.study.dto.StudentDTO;
import com.zzyy.study.entities.Student;
import com.zzyy.study.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther zzyy
 * @create 2024-06-13 10:44
 */
@RestController
public class StudentController
{
    @Resource
    private StudentService studentService;

    @PostMapping("/student/register")
    public Result<String> register(@Validated @RequestBody StudentDTO studentDTO)
    {
        return ResultUtils.success(studentService.register(studentDTO));
    }

    /**
     * 演示：事务中有异常，消息投递会被自动取消
     * @param studentDTO
     * @return
     */
    @PostMapping("/student/registerError")
    public Result<String> registerError(@Validated @RequestBody StudentDTO studentDTO) {
        return ResultUtils.success(studentService.registerError(studentDTO));
    }
}

/**

 ### 2024.6.16 right
 POST http://localhost:24616/student/register
 Content-Type: application/json

 {
 "name": "li4",
 "age": "12"
 }

 ### 2024.6.16 error
 POST http://localhost:24616/student/registerError
 Content-Type: application/json

 {
 "name": "sError",
 "age": "44"
 }


 ### 2024.6.16 send to RabbitMQ fail,let me try,if name contains error2024,throw exception
 POST http://localhost:24616/student/register
 Content-Type: application/json

 {
 "name": "xxxerror2024abc2",
 "age": "444"
 }

 */